package com.example.hanchen.yongkanggy.home.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.hanchen.yongkanggy.BR;
import com.example.hanchen.yongkanggy.R;
import com.example.hanchen.yongkanggy.databinding.ActivityMainBinding;
import com.example.hanchen.yongkanggy.home.base.StatusBarDatabindingActivity;
import com.example.hanchen.yongkanggy.home.fragment.ControlFragment;
import com.example.hanchen.yongkanggy.home.fragment.HomeFragment;
import com.example.hanchen.yongkanggy.home.fragment.MeFragment;
import com.example.hanchen.yongkanggy.home.fragment.WarnFragment;
import com.example.hanchen.yongkanggy.home.viewmodel.HomeViewModel;

import java.util.ArrayList;
import java.util.List;

/*
* 首页
* */

public class HomeActivity extends StatusBarDatabindingActivity<ActivityMainBinding, HomeViewModel> {

    protected List<Fragment> fragmentList;

    HomeFragment homeFragment;
    ControlFragment controlFragment;
    WarnFragment warnFragment;
    MeFragment meFragment;

    @Override
    protected int initLayoutID(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.homeViewModel;
    }

    @Override
    protected void initView() {
        homeFragment = HomeFragment.newInstance();
        controlFragment = ControlFragment.newInstance();
        warnFragment = WarnFragment.newInstance();
        meFragment = MeFragment.newInstance();
        showFragment(R.id.home_activity_framelayout, homeFragment);
    }

    @Override
    protected void initData() {

        /*首页*/
        findViewById(R.id.rlHome).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabinding.tvHome.setTextColor(getResources().getColor(R.color.color_236EFF));
                mDatabinding.tvControl.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvWarn.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvMe.setTextColor(getResources().getColor(R.color.color_666666));

                mDatabinding.ivHome.setImageResource(R.mipmap.home_y);
                mDatabinding.ivControl.setImageResource(R.mipmap.control_n);
                mDatabinding.ivWarn.setImageResource(R.mipmap.warn_n);
                mDatabinding.ivMe.setImageResource(R.mipmap.me_n);

                showFragment(R.id.home_activity_framelayout, homeFragment);
            }
        });

        /*监控*/
        findViewById(R.id.rlControl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabinding.tvHome.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvControl.setTextColor(getResources().getColor(R.color.color_236EFF));
                mDatabinding.tvWarn.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvMe.setTextColor(getResources().getColor(R.color.color_666666));

                mDatabinding.ivHome.setImageResource(R.mipmap.home_n);
                mDatabinding.ivControl.setImageResource(R.mipmap.control_y);
                mDatabinding.ivWarn.setImageResource(R.mipmap.warn_n);
                mDatabinding.ivMe.setImageResource(R.mipmap.me_n);

                showFragment(R.id.home_activity_framelayout, controlFragment);
            }
        });

        /*预警*/
        findViewById(R.id.rlWarn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabinding.tvHome.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvControl.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvWarn.setTextColor(getResources().getColor(R.color.color_236EFF));
                mDatabinding.tvMe.setTextColor(getResources().getColor(R.color.color_666666));

                mDatabinding.ivHome.setImageResource(R.mipmap.home_n);
                mDatabinding.ivControl.setImageResource(R.mipmap.control_n);
                mDatabinding.ivWarn.setImageResource(R.mipmap.warn_y);
                mDatabinding.ivMe.setImageResource(R.mipmap.me_n);

                showFragment(R.id.home_activity_framelayout, warnFragment);
            }
        });

        /*我的*/
        findViewById(R.id.rlMe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDatabinding.tvHome.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvControl.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvWarn.setTextColor(getResources().getColor(R.color.color_666666));
                mDatabinding.tvMe.setTextColor(getResources().getColor(R.color.color_236EFF));

                mDatabinding.ivHome.setImageResource(R.mipmap.home_n);
                mDatabinding.ivControl.setImageResource(R.mipmap.control_n);
                mDatabinding.ivWarn.setImageResource(R.mipmap.warn_n);
                mDatabinding.ivMe.setImageResource(R.mipmap.me_y);

                showFragment(R.id.home_activity_framelayout, meFragment);
            }
        });
    }


    protected void showFragment(int containerViewId, Fragment targetFragment) {
        if (fragmentList == null) {
            fragmentList = new ArrayList<>();
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : fragmentList) {
            ft.hide(fragment);
            fragment.onPause();
        }

        if (targetFragment.isAdded()) {
            targetFragment.onResume();
            ft.show(targetFragment);
        } else {
            ft.add(containerViewId, targetFragment);
            fragmentList.add(targetFragment);
        }

        ft.commitAllowingStateLoss();
    }
}